# TODO: 3D图绘制
# DATE: 2022/3/30
# AUTHOR: Cheng Ze WUST
import math
import random
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# fig=plt.figure()
# ax=Axes3D(fig,auto_add_to_figure=False)
# fig.add_axes(ax)
# x=np.arange(-4,4,0.25)
# y=np.arange(-4,4,0.25)
# X,Y=np.meshgrid(x,y)    #生成网格
# Z=np.sin(np.sqrt(x**2+y**2))
# ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
# #ax.plot_trisurf(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
# plt.show()


fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
plt.show()


fig=plt.figure()
ax=fig.gca(projection='3d')
theta=np.linspace(-4*np.pi,4*np.pi,100)
z=np.linspace(-2,2,100)
r=z**2+1
x=r*np.sin(theta)
y=r*np.cos(theta)
ax.plot(x,y,z)
plt.title('Fig2')
plt.show()


#np.random.seed(1)
def randrange(n,vmin,vmax):
    return (vmax-vmin)*np.random.rand(n)+vmin
fig=plt.figure()
ax=fig.gca(projection='3d')
n=100
for c,m,zlow,zhigh in [('r','o',-50,-25),('b','x',-30,-5)]:
    xs = randrange(n,23,32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs,ys,zs,c=c,marker=m)
ax.view_init(16) #初始角度位置
plt.title('Fig3')
plt.show()


fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
for c,z in zip(['r','g','b','y'],[30,20,10,0]):
    xs=np.arange(20)
    ys=np.random.rand(20)
    ax.bar(xs,ys,zs=z,zdir='y',alpha=0.8)
plt.title('Fig4')
plt.show()


